/**
 * @file LTC2943.c
 * @author 蔡云蔚 (you@domain.com)
 * @brief 库仑计LTC2943
 * @brief 注意：
 * @brief 本人实验中发现，读取只能从开头的00寄存器读取！！
 * @brief 无论读取时序中发送的寄存器值！
 * @brief 非常奇怪！！！！
 * @version 0.1
 * @date 2024-08-29
 * 
 * @copyright Copyright (c) 2024
 * 
 */

#include "ltc2943.h"



//寄存器的地址

constexpr uint8_t Status_A = 0X00;//状态寄存器 R 默认 01h

constexpr uint8_t Control_B =0x01; //控制寄存器 R/W 默认3Ch 00 111 10 0 

constexpr uint8_t Accumulated_Charge_MSB_C =0x02;//积累的电荷高8位 R/W 默认 7Fh
constexpr uint8_t Accumulated_Charge_LSB_D =0x03;//积累的电荷低8位 R/W 默认 FFh
constexpr uint8_t Charge_Threshold_High_MSB_E =0x04;//电荷阈值高高8位 R/W FFh
constexpr uint8_t Charge_Threshold_High_LSB_F =0x05;//电荷阈值高低8位 R/W FFh
constexpr uint8_t Charge_Threshold_Low_MSB_G =0x06;//电荷阈值高高8位 R/W 00h
constexpr uint8_t Charge_Threshold_Low_LSB_H =0x07;//电荷阈值高高8位 R/W 00h

constexpr uint8_t Voltage_MSB_I =0x08;//电压高8位 R 00h
constexpr uint8_t Voltage_LSB_J =0x09;//电压低8位 R 00h
constexpr uint8_t Voltage_Threshold_High_MSB_K =0x0A;//电压阈值高高8位 R/W 默认 FFh
constexpr uint8_t Voltage_Threshold_High_LSB_L =0x0B;//电压阈值高低8位 R/W 默认 FFh
constexpr uint8_t Voltage_Threshold_Low_MSB_M =0x0C;//电压阈值低高8位 R/W 默认 00h
constexpr uint8_t Voltage_Threshold_Low_LSB_N =0x0D;//电压阈值低低8位 R/W 默认 00h

constexpr uint8_t Current_MSB_O =0x0E;//电流高8位 R 默认 00h
constexpr uint8_t Current_LSB_P =0x0F;//电流低8位 R 默认 00h
constexpr uint8_t Current_Threshold_High_MSB_Q =0x10;//电流阈值高高8位 R/W 默认 FFh
constexpr uint8_t Current_Threshold_High_LSB_R =0x11;//电流阈值高低8位 R/W 默认 FFh
constexpr uint8_t Current_Threshold_Low_MSB_S =0x12;//电流阈值低高8位 R/W 默认 00h
constexpr uint8_t Current_Threshold_Low_LSB_T =0x13;//电流阈值低低8位 R/W 默认 00h

constexpr uint8_t Temperature_MSB_U =0x14;//温度高8位 R 默认 00h
constexpr uint8_t Temperature_LSB_V =0x15;//温度低8位 R 默认 00h
constexpr uint8_t Temperature_Threshold_High_W =0x16;//温度阈值高 R/W 默认 FFh
constexpr uint8_t Temperature_Threshold_Low_X =0x17;//温度阈值低 R/W 默认 00h



LTC2943::LTC2943(){ }

LTC2943 LTC2943::only;//初始化静态成员

LTC2943& LTC2943::getInstance()
{
    return only;
}

LTC2943& ltc2943 = LTC2943::getInstance();

void LTC2943::Init(void)
{
    LTC2943_I2C_INIT();


}



HAL_StatusTypeDef LTC2943::Status_Read(uint8_t* theStatus)
{
    HAL_StatusTypeDef res;
    uint8_t theRegister = Status_A;
    res = HAL_I2C_Master_Transmit(&LTC2943_I2C_Handle, LTC2943_ADDRESS, &theRegister, 1, HAL_MAX_DELAY);
    res = HAL_I2C_Master_Receive(&LTC2943_I2C_Handle, LTC2943_ADDRESS, theStatus, 1, HAL_MAX_DELAY);
    return res;
}

HAL_StatusTypeDef LTC2943::All_Read(uint8_t* theBuffer)
{
    HAL_StatusTypeDef res;
    uint8_t theRegister = Status_A;
    res = HAL_I2C_Master_Transmit(&LTC2943_I2C_Handle, LTC2943_ADDRESS, &theRegister, 1, HAL_MAX_DELAY);
    res = HAL_I2C_Master_Receive(&LTC2943_I2C_Handle, LTC2943_ADDRESS, theBuffer, 24, HAL_MAX_DELAY);
    return res;
}


HAL_StatusTypeDef LTC2943::Set_Control_Register(uint8_t config)
{
    HAL_StatusTypeDef res;
    uint8_t transmitBuffer[2] = { Control_B, config};
    res = HAL_I2C_Master_Transmit(&LTC2943_I2C_Handle, LTC2943_ADDRESS, transmitBuffer, sizeof(transmitBuffer), HAL_MAX_DELAY);
    if(res == HAL_OK)
    {
        Control_Register_record = config;//记录配置

        //111也是4096，请看说明书
        uint8_t tem = (Control_Register_record & 0X3C) >> 3;
        if (tem > 0X06) tem = 0X06;
        Prescaler_record = 0X0001 << (2 * tem);//记录预分配系数
        q_LSB_record = 0.34 * 50.0 / R_SENSE * Prescaler_record / 4096;//一量化单位代表的电量 单位：毫安时 mAh
    }
    return res;
}

// HAL_StatusTypeDef LTC2943_Voltage_Read(float* Voltage)
// {
//     HAL_StatusTypeDef res;
//     uint8_t theRegister = Voltage_MSB_I;
//     memPrint(&theRegister, 1);
//     uint8_t theBuffer[2] = { 0X00 };
//     res = HAL_I2C_Master_Transmit(&LTC2943_I2C_Handle, LTC2943_ADDRESS, &theRegister, 1, 1000);
//     if(res != HAL_OK) return res;
//     res = HAL_I2C_Master_Receive(&LTC2943_I2C_Handle, LTC2943_ADDRESS, theBuffer, 2, 1000);
//     if(res != HAL_OK) return res;

//     memPrint(theBuffer, 2);
//     uint16_t RESULT = (theBuffer[0] << 8) | theBuffer[1];
//     *Voltage = RESULT * (23.6 / 65535);//单位：伏特 V
//     return res;
// }

// HAL_StatusTypeDef LTC2943_Current_Read(float* Current)
// {
//     HAL_StatusTypeDef res;
//     uint8_t theRegister = Current_MSB_O;
//     memPrint(&theRegister, 1);
//     uint8_t theBuffer[2] = { 0X00 };
//     res = HAL_I2C_Master_Transmit(&LTC2943_I2C_Handle, LTC2943_ADDRESS, &theRegister, 1, 1000);
//     if(res != HAL_OK) return res;
//     res = HAL_I2C_Master_Receive(&LTC2943_I2C_Handle, LTC2943_ADDRESS, theBuffer, 2, 1000);
//     if(res != HAL_OK) return res;

//     memPrint(theBuffer, 2);
//     uint16_t RESULT = (theBuffer[0] << 8) | theBuffer[1];
//     *Current = (RESULT - 32767) * (60.0 / R_SENSE / 32767);//单位：安培 A
//     return res;
// }

// HAL_StatusTypeDef LTC2943_Temperature_Read(float* Temperature)
// {
//     HAL_StatusTypeDef res;
//     uint8_t theRegister = Temperature_MSB_U;
//     memPrint(&theRegister, 1);
//     uint8_t theBuffer[2] = { 0X00 };
//     res = HAL_I2C_Master_Transmit(&LTC2943_I2C_Handle, LTC2943_ADDRESS, &theRegister, 1, 1000);
//     if(res != HAL_OK) return res;
//     res = HAL_I2C_Master_Receive(&LTC2943_I2C_Handle, LTC2943_ADDRESS, theBuffer, 2, 1000);
//     if(res != HAL_OK) return res;

//     memPrint(theBuffer, 2);
//     uint16_t RESULT = (theBuffer[0] << 8) | theBuffer[1];
//     *Temperature = RESULT * (510.0 / 65535) - 273.15;//得到摄氏温度℃
//     return res;
// }

// HAL_StatusTypeDef LTC2943_Accumulated_Charge_Read(float* Accumulated_Charge)
// {
//     HAL_StatusTypeDef res;
//     uint8_t theRegister = Accumulated_Charge_MSB_C;
//     memPrint(&theRegister, 1);
//     uint8_t theBuffer[2] = { 0X00 };
//     res = HAL_I2C_Master_Transmit(&LTC2943_I2C_Handle, LTC2943_ADDRESS, &theRegister, 1, 1000);
//     if(res != HAL_OK) return res;
//     res = HAL_I2C_Master_Receive(&LTC2943_I2C_Handle, LTC2943_ADDRESS, theBuffer, 2, 1000);
//     if(res != HAL_OK) return res;

//     memPrint(theBuffer, 2);
//     uint16_t RESULT = (theBuffer[0] << 8) | theBuffer[1];
//     *Accumulated_Charge = RESULT * q_LSB_record;//得到电量 单位：毫安时 mAh
//     return res;
// }

HAL_StatusTypeDef LTC2943::Accumulated_Charge_Set(float Accumulated_Charge)
{
    HAL_StatusTypeDef res;
    uint16_t RESULT = (uint16_t)(Accumulated_Charge / q_LSB_record);
    uint8_t transmitBuffer[3] = { Accumulated_Charge_MSB_C, (uint8_t)((RESULT & 0XFF00)>>8), (uint8_t)(RESULT & 0X00FF)};
    res = HAL_I2C_Master_Transmit(&LTC2943_I2C_Handle, LTC2943_ADDRESS, transmitBuffer, 3, HAL_MAX_DELAY);
    
    return res;
}

float LTC2943::Accumulated_Charge_get(uint8_t* Accumulated_Charge_Buffer)
{
    uint16_t RESULT = (Accumulated_Charge_Buffer[0] << 8) | Accumulated_Charge_Buffer[1];
    float Q_BAT = RESULT * q_LSB_record;
    return Q_BAT;
}

float LTC2943::Voltage_get(uint8_t* Voltage_Buffer)
{
    uint16_t RESULT = (Voltage_Buffer[0] << 8) | Voltage_Buffer[1];
    float V_SENSE = RESULT * (23.6 / 65535);//单位：伏特 V
    return V_SENSE;
}

float LTC2943::Current_get(uint8_t* Current_Buffer)
{
    uint16_t RESULT = (Current_Buffer[0] << 8) | Current_Buffer[1];
    float I_BAT = (RESULT - 32767) * (60.0 / R_SENSE / 32767);//单位：安培 A
    return I_BAT;
}

float LTC2943::Temperature_get(uint8_t* Temperature_Buffer)
{
    uint16_t RESULT = (Temperature_Buffer[0] << 8) | Temperature_Buffer[1];
    float Temperature = RESULT * (510.0 / 65535) - 273.15;//得到摄氏温度℃
    return Temperature;
}